from datetime import datetime

import yaml


def add(file, key, value):
    # 将结果写入指定文件
    with open(file, 'a', encoding='utf-8') as file:
        file.write(key + ": " + value + "\n")  # 添加换行符，使文件内容更易读


def duration(local_time, data, file):
    local_time_obj = datetime.strptime(local_time, "%Y-%m-%d %H:%M")
    mid_time_obj = datetime.strptime(data['mid_time'], "%Y-%m-%d %H:%M")
    end_time_obj = datetime.strptime(data['end_time'], "%Y-%m-%d %H:%M")
    if local_time_obj > mid_time_obj:
        time_difference = end_time_obj - local_time_obj
    else:
        time_difference = mid_time_obj - local_time_obj

    # 提取天数、小时数和分钟数
    days = time_difference.days
    hours, remainder = divmod(time_difference.seconds, 3600)
    minutes, _ = divmod(remainder, 60)

    # 格式化时间差
    formatted_time_difference = f"{days}天{hours}小时{minutes}分钟"

    # 将结果写入指定文件
    with open(file, 'w', encoding='utf-8') as file:
        file.write("持续时间： " + formatted_time_difference + "\n")  # 添加换行符

    return formatted_time_difference


def pool(local_time, data, file):
    if isinstance(local_time, datetime):
        local_time_str = local_time.strftime("%Y-%m-%d %H:%M")
    else:
        local_time_str = local_time

    local_time_obj = datetime.strptime(local_time_str, "%Y-%m-%d %H:%M")
    mid_time_obj = datetime.strptime(data['mid_time'], "%Y-%m-%d %H:%M")
    if local_time_obj < mid_time_obj:
        add(file, "五星角色", str(data['up_five_star_char']).replace('&', '、'))
        add(file, "五星武器", str(data['up_five_star_weapon']).replace('&', '、'))
        add(file, "四星角色", str(data['up_four_star_char']).replace('&', '、'))
        add(file, "四星武器", str(data['up_four_star_weapon']).replace('&', '、'))


def update_time(time, value):
    if len(value) == len('01-23'):
        return f'{time[0:5]}{value} {time[11:]}'
    if len(value) == len('25-01-23'):
        return f'{time[0:2]}{value} {time[11:]}'
    if len(value) == len('2025-01-23'):
        return f'{value} {time[11:]}'
    if len(value) == len('2025-01-23 09:59'):
        return value
    else:
        return "输入的时间格式不正确，请使用 MM-DD 或 YY-MM-DD 或 YYYY-MM-DD 格式"


def update(file, key, value):
    # 读取yaml文件
    with open(file, 'r', encoding='utf-8') as f:
        data = yaml.safe_load(f)
    if key in '上半卡池结束时间':
        result = update_time(data['mid_time'], value)
        print(result)
        if len(result) == len('2025-01-23 09:59'):
            data['mid_time'] = result
        else:
            return result
    elif key in '下半卡池结束时间':
        result = update_time(data['end_time'], value)
        print(result)
        if len(result) == len('2025-01-23 09:59'):
            data['end_time'] = result
        else:
            return result
    elif key in '上半五星角色':
        data['up_five_star_char'] = value
    elif key in '下半五星角色':
        data['down_five_star_char'] = value
    elif key in '上半四星角色':
        data['up_four_star_char'] = value
    elif key in '下半四星角色':
        data['down_four_star_char'] = value
    elif key in '上半五星武器':
        data['up_five_star_weapon'] = value
    elif key in '下半五星武器':
        data['down_five_star_weapon'] = value
    elif key in '上半四星武器':
        data['up_four_star_weapon'] = value
    elif key in '下半四星武器':
        data['down_four_star_weapon'] = value
    with open(file, 'w', encoding='utf-8') as f:
        yaml.dump(data, f, allow_unicode=True, sort_keys=False)
    with open(file, 'r', encoding='utf-8') as f:
        data = yaml.safe_load(f)
    if key in '上半卡池结束时间':
        return "修改的内容为：" + key + ": " + data['mid_time']
    elif key in '下半卡池结束时间':
        return "修改的内容为：" + key + ": " + data['end_time']
    elif key in '上半五星角色':
        return "修改的内容为：" + key + ": " + data['up_five_star_char']
    elif key in '下半五星角色':
        return "修改的内容为：" + key + ": " + data['down_five_star_char']
    elif key in '上半四星角色':
        return "修改的内容为：" + key + ": " + data['up_four_star_char']
    elif key in '下半四星角色':
        return "修改的内容为：" + key + ": " + data['down_four_star_char']
    elif key in '上半五星武器':
        return "修改的内容为：" + key + ": " + data['up_five_star_weapon']
    elif key in '下半五星武器':
        return "修改的内容为：" + key + ": " + data['down_five_star_weapon']
    elif key in '上半四星武器':
        return "修改的内容为：" + key + ": " + data['up_four_star_weapon']
    elif key in '下半四星武器':
        return "修改的内容为：" + key + ": " + data['down_four_star_weapon']


if __name__ == '__main__':
    file = "pool.txt"
    # 获取当前本地时间并格式化，省去秒
    local_time = datetime.now()
    formatted_time = local_time.strftime("%Y-%m-%d %H:%M")

    # 读取YAML文件，指定编码为utf-8
    with open("pool.yaml", 'r', encoding='utf-8') as stream:
        data = yaml.safe_load(stream)

    # 调用duration函数
    duration(formatted_time, data, file)

    # 调用pool函数
    pool(formatted_time, data, file)
